﻿
using Blog.IRepository;
using Blog.IServices;
using Model.Models;
using Blog.Core3.Services.BASE;
using System.Threading.Tasks;
using System.Collections.Generic;
using Blog.Core3.Model.VeiwModels;
using Blog.Common.Redis;
using System.Linq.Expressions;
using System;
using AutoMapper;
using Blog.Core3.Model;

namespace Blog.Services
{
    /// <summary>
    ///  博客 服务层
    /// </summary>
    public partial class ArticleInfoServices : BaseServices<ArticleInfo>, IArticleInfoServices
    {
        IArticleInfoRepository _dal;
        readonly IRedisCacheManager redisCacheManager; //缓存 
        IMapper imapper;  //dto
        public ArticleInfoServices(IArticleInfoRepository dal, IRedisCacheManager _redisCacheManager, IMapper _imapper)
        {
            this._dal = dal;
            base.BaseDal = dal;
            this.redisCacheManager = _redisCacheManager;
            imapper = _imapper;
        }

        /// <summary>
        /// 根据账户获取博客文章
        /// </summary>
        /// <param name="account"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public async Task<object> GetByAccountBlog(decimal account, int page, int size)
        {
            //redis-server  redis.windows.conf  Redis 启动命令

            PageModel<ArticleInfo> blogArticleList;
            int AccountblogCount = 0;
            List<BlogArticleShowViewModel> models;
            //获取 总共博客总数
            if (redisCacheManager.Get($"Redis.Blog.Count"))
            {
                AccountblogCount = redisCacheManager.Get<int>($"Redis.Blog.Count");
            }
            if (redisCacheManager.Get($"Redis.Blog.{account}.{page}"))
            {
            }

            if (redisCacheManager.Get($"Redis.Blog.{account}.{page}"))
            {
                blogArticleList = redisCacheManager.Get<PageModel<ArticleInfo>>($"Redis.Blog.{account}.{page}");
                AccountblogCount = redisCacheManager.Get<int>($"Redis.Blog.Count");
            }
            else
            {
                // 表达数
                Expression<Func<ArticleInfo, bool>> whereExpression = a =>
                (a.ArticleIsDelete == 0
                && a.ArticleIsRelease == 1
                && a.UserAccount == account.ToString()
                );
                // 根据分页查询出当前的数据
                blogArticleList = await _dal. QueryPage(whereExpression, page, size);
                foreach (var item in blogArticleList.data)
                {
                    item.ArticleBody = item.ArticleBody.Substring(0, item.ArticleBody.Length / 2);

                } 

                var Accountblog = await _dal.Query(whereExpression);
                redisCacheManager.Set($"Redis.Blog.Count", Accountblog.Count, TimeSpan.FromHours(2));//缓存2小时 

                redisCacheManager.Set($"Redis.Blog.{account}.{page}", blogArticleList, TimeSpan.FromHours(2));//缓存2小时 
            }
            models = imapper.Map<List<BlogArticleShowViewModel>>(blogArticleList);

            var obj = new
            {
                Count = AccountblogCount,
                Page = page,
                Success = true,
                Data = models  
            }; 
            return obj;
        } 
    }
}
                    